import { deepCopy } from "@/views/board/BoardEdit/LowCode/lib/utils"

const snapshot = {
  state: {
    snapshotList: [],
    snapshotIndex: -1,
    snapshotMinIndex: 0, // 最小的index.在编辑状态下,初始化就会触发一次addSnapshot,因此最小index为1
  },
  mutations: {
    addSnapshot(state, data){
      // 判断数据是否和上一次记录相同,如果相同就不记录
      const { canvasComponentList,pageSetting } = data
      // if(state.snapshotList.length <= 0 || index === -1 || JSON.stringify(canvasComponentList[index]) !== JSON.stringify(state.snapshotList[state.snapshotList.length -1][index])){
        // 如果当前处于最后一次快照
        if(state.snapshotIndex === state.snapshotList.length - 1){
          state.snapshotList.push({
            canvasComponentList: deepCopy(canvasComponentList || []),
            pageSetting: deepCopy(pageSetting)
          })
        }
        else {
          state.snapshotList.splice(state.snapshotIndex + 1, state.snapshotList.length - state.snapshotIndex - 1, {
            canvasComponentList: deepCopy(canvasComponentList || []),
            pageSetting: deepCopy(pageSetting)
          })
        }

        state.snapshotIndex += 1
      // }
      // else {
      //   // console.log("无改动")
      // }


    },
    snapshotNext(state){
      state.snapshotIndex += 1
    },
    snapshotPrev(state){
      state.snapshotIndex -= 1
    },
    setSnapshotMinIndex(state, index) {
      state.snapshotMinIndex = index
      if(state.snapshotIndex < state.snapshotMinIndex - 1){
        state.snapshotIndex = index - 1
      }

    },
    resetSnapshot(state){
      state.snapshotList = []
      state.snapshotIndex = -1
    }
  }
}

export default snapshot
